package com.rizzo.web.facelets;

import com.rizzo.web.util.EnumUtil;
import com.rizzo.web.util.MessageUtil;
import com.rizzo.web.util.SecurityUtil;
import com.sun.facelets.tag.AbstractTagLibrary;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * The definition of the Facelets Function-library for use on the views.
 *
 * @author Jan Lievens
 */
public class FunctionLibrary extends AbstractTagLibrary {

    public static final String NAMESPACE = "http://www.ganymed.be/gouse/functions";

    /**
     * Default constructor.
     */
    public FunctionLibrary() {
        super(NAMESPACE);
        register(SecurityUtil.class);
        register(MessageUtil.class);
        register(EnumUtil.class);
    }

    /**
     * Registers a certain class with the configured {@link com.sun.facelets.tag.AbstractTagLibrary}.
     *
     * @param utilClass The utility-class to register.
     */
    private void register(Class<?> utilClass) {
        try {
            Method[] methods = utilClass.getMethods();

            for (Method method : methods) {
                if (Modifier.isStatic(method.getModifiers())) {
                    this.addFunction(method.getName(), method);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}